<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zyh" @change="zyChange">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.yjfxm" @change="fxChange">
                <a-select-option v-for="i in selectMenu.fx" :key="i.id" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="导师">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.dsxxmId">
                <a-select-option v-for="i in selectMenu.ds" :key="i.id" :value="i.id">{{ i.gh+'-'+i.xm }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="招生年度">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsxn" disabled>
                <a-select-option v-for="i in TYPE0387" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          </template>
          <a-col :sm="{span:24}" style="text-align:center;width:40%;padding-top:29px;" >          
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
          </a-col>
        </a-row>
      </a-form>
    </div>
    </a-card>
  <a-card class="card" :bordered="false">
    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :alert="options.alert"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :pagination="pagination"
			@change="handleTableChange"
      :scroll="{ x: 1500 }"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
  </a-card>
</div>
</template>

<script>
// import moment from 'moment'

import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0387']

export default {
  name: 'Recruit',
  components: {
    STable,
  },
  data () {
    return {
      advanced: true,
      loading: false,
      data: [],
      // 查询参数
      queryParam: {
        zsxn:'',
        ssyxm:'',
        sbzt:'1'
      },
      pagination:{
        current:1
      },
       selectedRowKeys: [],
      selectedRows:'',
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '院系',
          dataIndex: 'dwmc'
        },
        {
          title: '专业',
          dataIndex: 'zymc'
        },
        {
          title: '招生年度',
          dataIndex: 'zsxn'
        },
        {
          title: '研究方向',
          dataIndex: 'yjfxmc'
        },
        {
          title: '导师',
          dataIndex: 'xm'
        },
        {
          title: '招生人数',
          dataIndex: 'zsrs'
        },
        {
          title: '考试科目',
          dataIndex: 'kskm'
        },
        {
          title: '备注',
          dataIndex: 'note'
        }
      ],

      // custom table alert & rowSelection
      options: {
        alert: { show: false },
      },
      recruitAddVisiable: false,
      recruitEditVisiable: false,
      selectMenu: {
        yx: [], // 院系
        zy: [],
        fx: [],
        ds: []
      },
      recruitEditData: {}
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0387: state => state.app['TYPE0387'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
   
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.ssyxm=res[0].data[0].dwh;
        this.$api.base.majorAll({lsdwh: this.queryParam.ssyxm}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      this.$api.base.getSchoolYear().then(res=>{
        this.queryParam.zsxn=res.data.doctoralAdmissionYear;
        this.loadData({
        pageNum:1,
        pageSize:10
      })
      })
      })
      
       
    },
     // 选择院系
    yxChange (code) {
      this.$set(this.queryParam,'zyh','')
      this.$set(this.queryParam,'yjfxm','')
      this.$set(this.queryParam,'dsxxmId','')
      this.$set(this.selectMenu,'zy','')
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
       this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
      }
    },
    zyChange (code) {
       this.$set(this.queryParam,'yjfxm','')
      this.$set(this.queryParam,'dsxxmId','')
      this.$set(this.selectMenu,'fx','')
      this.$set(this.selectMenu,'ds','')
      if(code){
      let params={
        ssyxm:this.queryParam.ssyxm,
        sszym:this.queryParam.zyh,
        xwccm:3,
        zsxn:this.queryParam.zsxn
      }
      this.$api.base.researchAll(params).then(res=>{
        this.$set(this.selectMenu,'fx',res.data)
      })
      }
    },
    //选择方向
    fxChange(code){
      this.$set(this.queryParam,'dsxxmId','')
      this.$set(this.selectMenu,'ds','')
      if(code){
      let params={
        ssyxm:this.queryParam.ssyxm,
        yjfxm:this.queryParam.yjfxm,
        zym:this.queryParam.zyh,
        zsxn:this.queryParam.zsxn
      } 
      this.$api.base.getGuide(params).then(res=>{
        this.$set(this.selectMenu,'ds',res.data)
      })
      }
    },
    // 加载数据
     loadData(parameter = {}) {
       if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.doctorRecruit.CollegeStudentList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
        }
        this.loading = false;
      })
    },
    // 分页
			handleTableChange(pagination, filters, sorter) {
				console.log(pagination);
				const pager = { ...this.pagination
				};
				pager.current = pagination.current;
				this.pagination = pager;
				this.loadData({
					pageSize: 10,
					pageNum: pagination.current
				});
			},
    // 选择招生年度
    zsndChange () {

    },
    // 添加
    handleCreate () {
      this.recruitAddVisiable = true
    },
    // 关闭添加弹层
    handleCreateClose () {
      this.recruitAddVisiable = false
    },
    // 添加成功
    handleCreateSuccess () {
      this.recruitAddVisiable = false
      this.$message.success('添加成功')
      this.loadData({
        pageNum:this.pagination.current,
        pageSize:10
      })
    },
    // 修改
    handleEdit (key) {
      this.$api.doctorRecruit.CollegeStudentId(key).then(res=>{
        this.recruitEditData=res.data;
      })
      this.recruitEditVisiable = true
    },
    // 关闭添加弹层
    handleEditClose () {
      this.recruitEditVisiable = false
    },
    // 添加成功
    handleEditSuccess () {
      this.recruitEditVisiable = false
      this.$message.success('修改成功')
      this.loadData({
        pageNum:this.pagination.current,
        pageSize:10
      })
    },
    // 导出
    exportExcel () {
      this.$export('admissions/zsBsszszyml/export',this.queryParam)
    },
    // 删除
    handleDelete (key) {
      let _this=this;
      this.$confirm({
         title: '确定删除?',
         onOk() {
          _this.$api.doctorRecruit.officeDelete(key).then(res=>{
        if(res.status==1){
          _this.$message.success(res.msg)
          _this.loadData({
            pageSize:10,
            pageNum:_this.pagination.current
          })
        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });
      
    },
      onSelectChange (selectedRowKeys,selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows=selectedRows;
    },
    // 批量删除
    handleBatchDelete () {
      let _this=this;
     if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要报到的学生')
        return
      }

      this.$confirm({
         title: '确定删除?',
         onOk() {
           let para=[],params
      _this.selectedRows.forEach(item=>{
        para.push(item.id);
      })
      params=para.join(',')
      _this.$api.doctorRecruit.officeDelete(params).then(res=>{
        console.log(res.status)
        if (res.status==1) {
           _this.selectedRowKeys = [];
          _this.loadData({
					pageSize: 10,
					pageNum: _this.pagination.current
        });
        _this.$message.success(res.msg)
      
        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });
      
    },
    // 还原为未上报状态
    resetInit () {
      this.$api.doctorRecruit.officeCancel({zsxn:this.queryParam.zsxn,ssyxm:this.queryParam.ssyxm}).then(res =>{
        this.$message.success("操作成功")
      })
    },
    // 导出PDF
    exportPDF () {
      console.log('导出PDF')
      this.$export('admissions/zsBsszszyml/exportPdf',this.queryParam)
    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
